Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow prefetch to start on custom event #52574

Merged
merged 1 commit into from
Aug 26, 2024

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Aug 26, 2024

In my initial design of the asset prefetching feature, I included a delay before prefetching started. I removed the delay to simplify the design in the final version.

Having not yet documented the feature, an initial delay has already being requested. I can see the advantage of having control of this.

Instead of introducing an arbitrary delay or configurable delay, I've instead opted for a configurable event name that triggers prefetching.

This allows for a custom delay or more dynamic prefetching control, e.g., start after an initial request.

// Service provider...

use Illuminate\Support\Facades\Vite;

Vite::prefetch(
    concurrency: 3,
    event: 'vite:prefetch',
]);

then...

<script>
    // prefetch Vite assets 1 second after the load event...

    addEventListener('load', () => {
        setTimeout(() => dispatchEvent(new Event('vite:prefetch')), 1000)
    })
</script>

or ...

<script>
    // prefetch Vite assets after downloading the user's profile information...

    const profile = await axios.get('/me').then(() => dispatchEvent(new Event('vite:prefetch'))
    
    // ...
</script>

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@timacdonald timacdonald marked this pull request as ready for review August 26, 2024 00:15
@timacdonald timacdonald changed the title [11.x] Allow prefetch to happen on custom event [11.x] Allow prefetch to start on custom event Aug 26, 2024
@taylorotwell taylorotwell merged commit 99ae397 into laravel:11.x Aug 26, 2024
31 checks passed
@timacdonald timacdonald deleted the prefetch-delay branch August 26, 2024 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants